﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace AUV5.Web.Pages
{
    public partial class Withdraw : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    BindData();
                }
                else
                {
                    Response.Redirect(URL_INDEX);
                }
            }
        }

        #region##绑定数据
        /// <summary>
        /// 绑定数据
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        private void BindData()
        {
            AUV5.Model.UserAccount userAccount = AUV5.BLL.UserAccountBLL.GetInstance().GetUserAccountByUserId(VerifiedUserID);

            if (userAccount != null)
            {
                lblAccount.Text = userAccount.Account;

                AUV5.Model.AccountType accountType = AUV5.BLL.AccountTypeBLL.GetInstance().GetDetail(userAccount.AccountTypeID);

                if (accountType != null)
                {
                    lblAccountType.Text = accountType.AccountTypeName;
                }
            }

            decimal balance = AUV5.BLL.OrderInfoBLL.Instance.GetBalanceByUserId(VerifiedUserID);   //余额

            lblBalance.Text = String.Format("{0:f2}", balance);

            if (balance < 50)
            {
                btnSubmit.Enabled = false;
            }
        }
        #endregion

        #region##提交
        /// <summary>
        /// 提交
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSub_Click(object sender, EventArgs e)
        {
            try
            {
                Regex re = new Regex("^(([5-9])|([1-9][0-9]))\\d+$", RegexOptions.None);
                if (!re.IsMatch(txtAmount.Text.Trim()))
                {
                    Alert("提现金额只能输入大于等于50的正整数");
                }
                else
                {
                    AUV5.Model.UserAccount userAccount = AUV5.BLL.UserAccountBLL.GetInstance().GetUserAccountByUserId(VerifiedUserID);

                    AUV5.Model.WithDrawLog log = new Model.WithDrawLog();
                    if (userAccount != null)
                    {
                        log.Account = userAccount.Account;

                        AUV5.Model.AccountType accountType = AUV5.BLL.AccountTypeBLL.GetInstance().GetDetail(userAccount.AccountTypeID);                        
                        log.AccountTypeID = userAccount.AccountTypeID;

                        log.Amount = Convert.ToDecimal(txtAmount.Text.Trim());
                        log.OpenAccountAdress = userAccount.OpenAccountAdress;
                        log.PhoneNo = userAccount.PhoneNo;
                        log.RealName = userAccount.RealName;
                        log.UserID = VerifiedUserID;
                        log.WithdrawLogID = Guid.NewGuid().ToString();
                        AUV5.BLL.WithDrawLogBLL.GetInstance().Add(log);
                    }
                    

                }
                AlertGo("提交成功", "WithdrawList.aspx");
            }
            catch
            {
                Alert("提交失败");
            }
        }
        #endregion
    }
}